import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.odps.utils.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    public static void main(String[] args) throws Exception {
        HttpClientAPI httpClientAPI = new HttpClientAPI();
        HttpResult htmlResult = httpClientAPI.doGet("https://www.cultbeauty.co.uk/augustinus-bader-the-creamthe-rich-cream-travel-size-duo-worth-250.html");
        Document doc =Jsoup.parse(htmlResult.getBody()) ;
        JSONObject returnObj = new JSONObject();
        //获取部分描述内容
        Elements descp = doc.getElementsByClass("productInfoInner");
        if(null != descp && descp.size() == 1) {
            Elements liTags = descp.get(0).getElementsByTag("li");
            if(null != liTags && liTags.size() != 0) {
                for (int i = 0; i < 4; i++)  {
                    Element element = liTags.get(i);
                    //获取标题
                    String head = element.getElementsByClass("itemHeader").text();
                    System.out.println(head);
                    //处理成分
                    if(StringUtils.equals(head,"Full ingredients list")){
                        List<Map<String,String >> inredientList = new ArrayList<>();
                        Element itemContent = element.getElementsByClass("itemContent").get(0);
                        Elements pEles = itemContent.getElementsByTag("p");
                        for (Element e :pEles){
                            Map<String,String > ingreMap = new HashMap<>();
                            String text = e.text();
                            if(StringUtils.isNotBlank(text) && text.contains(":")){
                                ingreMap.put("goods_name",text.substring(0,text.indexOf(":")));
                                ingreMap.put("goods_ingredient",text.substring(text.indexOf(":")+1));
                            }else {
                                ingreMap.put("goods_name","");
                                ingreMap.put("goods_ingredient",text);
                            }
                            inredientList.add(ingreMap);
                        }
                        System.out.println(JSON.toJSONString(inredientList));
                        returnObj.put(head,inredientList );
                    }else{
                        //获取内容 内容比较多根据标签p分段
                        Element itemContent = element.getElementsByClass("itemContent").get(0);
                        Elements pEles = itemContent.getElementsByTag("p");
                        StringBuffer sbContent = new StringBuffer();
                        for (Element e :pEles){
                            sbContent.append(e.text()).append("@@");
                        }
                        String content = sbContent.substring(0,sbContent.lastIndexOf("@@"));
                        returnObj.put(head, content);
                    }

                }
            }
        }
        System.out.println(returnObj.toJSONString());

    }


}
